﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using HYCodeBase.Math;

namespace HYRevitCode.ElementCreatUtility.MultilineTextCreat
{
	internal class TextColl
	{
		internal static bool CreatTextCollByType(Autodesk.Revit.DB.Document useDoc, Autodesk.Revit.DB.View useView, AlignType align, TextNoteType useTextType, List<string> lstText, out List<TextNote> lstTextNote)
		{
			lstTextNote = new List<TextNote>();
			if (useTextType == null)
			{
				return false;
			}
			bool result;
			try
			{
				TextNoteOptions textNoteOptions = new TextNoteOptions();
				ElementId typeId = new FilteredElementCollector(useDoc).OfClass(typeof(TextNoteType)).ToElementIds().ToList<ElementId>().First<ElementId>();
				textNoteOptions.TypeId = typeId;
				lstText = (from s in lstText
				where !string.IsNullOrWhiteSpace(s)
				select s).ToList<string>();
				for (int i = 0; i < lstText.Count; i++)
				{
					TextNote textNote = TextNote.Create(useDoc, useView.Id, XYZ.Zero, lstText[i], textNoteOptions);
					textNote.TextNoteType = useTextType;
					lstTextNote.Add(textNote);
				}
				result = true;
			}
			catch (Exception)
			{
				lstTextNote = null;
				result = false;
			}
			return result;
		}

		internal static bool CreatTextColl(Autodesk.Revit.DB.Document useDoc, Autodesk.Revit.DB.View useView, AlignType align, string font, double size, List<string> lstText, out List<TextNote> lstTextNote)
		{
			bool result;
			try
			{
				if (MathUtility.Lessthan_Or_Equal(size, 0.0))
				{
					throw new ArgumentOutOfRangeException();
				}
				lstTextNote = new List<TextNote>();
				TextNoteOptions textNoteOptions = new TextNoteOptions();
				ElementId typeId = new FilteredElementCollector(useDoc).OfClass(typeof(TextNoteType)).ToElementIds().ToList<ElementId>().First<ElementId>();
				textNoteOptions.TypeId = typeId;
				lstText = (from s in lstText
				where !string.IsNullOrWhiteSpace(s)
				select s).ToList<string>();
				for (int i = 0; i < lstText.Count; i++)
				{
					TextNote textNote = TextNote.Create(useDoc, useView.Id, XYZ.Zero, lstText[i], textNoteOptions);
					if (textNote != null)
					{
						Parameter parameter = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE);
						if (parameter != null)
						{
							parameter.Set(size);
						}
						Parameter parameter2 = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_FONT);
						if (parameter2 != null)
						{
							parameter2.Set(font);
						}
						Parameter parameter3 = textNote.get_Parameter(BuiltInParameter.TEXT_ALIGN_HORZ);
						if (parameter3 != null)
						{
							int num = (align.GetHashCode() % 2 == 0) ? 64.GetHashCode() : 256.GetHashCode();
							parameter3.Set(num);
							lstTextNote.Add(textNote);
						}
					}
				}
				result = true;
			}
			catch (Exception)
			{
				lstTextNote = null;
				result = false;
			}
			return result;
		}

		internal static bool SetPosition(Autodesk.Revit.DB.View currentView, List<TextNote> lstTextNote, double dRrowPitch)
		{
			if (lstTextNote.Count.Equals(0))
			{
				return false;
			}
			if (lstTextNote.Count.Equals(1))
			{
				lstTextNote.First<TextNote>().Coord = XYZ.Zero;
				return true;
			}
			double num = 0.0;
			for (int i = 1; i < lstTextNote.Count; i++)
			{
				double coorOffset = TextColl.GetCoorOffset(lstTextNote[i - 1], lstTextNote[i], currentView, dRrowPitch);
				num += coorOffset;
				XYZ xyz = -currentView.UpDirection;
				ElementTransformUtils.MoveElement(currentView.Document, lstTextNote[i].Id, xyz.Multiply(num));
			}
			return true;
		}

		private static double GetCoorOffset(TextNote referenceText, TextNote currentText, Autodesk.Revit.DB.View useView, double rowPitch)
		{
			return referenceText.Height * (double)useView.Scale * 0.875 + rowPitch;
		}

		internal const double TEXTDISTANCE = 0.875;
	}
}
